<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
require('include.php');
use Admin\Controller\Tencentyun\Conf;
use Admin\Controller\Tencentyun\ImageV2;
use Admin\Controller\Tencentyun\Auth;
use Admin\Controller\Tencentyun\ImageProcess;
class UserPropController extends AdminbaseController{
	
	function _initialize() {
		parent::_initialize();
	}
	
	function index(){
		$broadcastName = I("post.broadcastName", "");
		$startTime = I("post.startTime", "");
		$endTime = I("post.endTime", "");
		$page = $this->page(100, 20);
		$param = 'beginRow='.$page->firstRow.'&fetchRows='.$page->listRows.'&startTime='.$startTime.'&endTime='.$endTime.'&broadcastName='.$broadcastName;
		$result = $this->curl_http(C('video_url').'l_u_prop/queryList?token='.$_SESSION["TOKEN"], $param);
		$data = $result['ReturnData'];
		$page = $this->page($result['total'], 20);
		
		$ids = array();
	    foreach ($data as $v){
			$userId = number_format($v['userId'], 0, '', '');
			if(!in_array($userId, $ids)){
				$ids[] = number_format($v['userId'], 0, '', '');
			}
		}
		
		$idsStr = implode("," , $ids);
		$result = $this->curl_http(C('user_url').'user/getAppUsersByIds?userIds='.$idsStr);
		$userData = $result['ReturnData']['appUser'];
		
		$users = array();
		
		foreach ($userData as $v){
			$userId = number_format($v['userId'], 0, '', '');
			$users['nn_'.$userId] = $v['nickName'];
			$users['un_'.$userId] = $v['userName'];
		}
	    foreach ($data as &$v){
	    	$userId = number_format($v['userId'], 0, '', '');
			$v['nickname'] = $users['nn_'.$userId];
			$v['username'] = $users['un_'.$userId];
		}
		$this->assign("data", $data);
		$this->assign("formParam", array('startTime'=>$startTime, 'endTime'=>$endTime, 'broadcastName'=>$broadcastName));
		
		$this->display();
	}
	
	
	function add(){
		$this->display();
	}
	
	function add_post(){
		$name = I("post.name", "");
		if(empty($name)){
			return $this->error("道具名称不能为空！");
		}
		
		$amount = I("post.amount", '');
		if(empty($amount)){
			return $this->error("汇闻币数量不能为空！");
		}
		if (!preg_match('/^[1-9]+[0-9]*$/', $amount)) {
			return $this->error("汇闻币数量不合法（整数）！");
		}
		
		if(empty($_FILES)){
			return $this->error("请上传道具图标！");
		}
			
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     59614161 ;// 设置附件上传大小
		$upload->exts      =     array('jpeg', 'jpg', 'bmp', 'png');// 设置附件上传类型
		$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{
			$savename = C('UPLOADPATH').$info['imageFile']['savepath'].$info['imageFile']['savename'];
			$fileName = 'lv'.time().$info['imageFile']['savename'];  // 自定义文件名
			$uploadRet = ImageV2::upload($savename, Conf::BUCKET, $fileName);
			\Think\Log::record('万象优图返回：'.json_encode($uploadRet), 'INFO');
			$imageUrl = $uploadRet['data']['downloadUrl'];
			$status = I("post.status", 1); 
			$remark = I("post.remark", "");
			$param = 'name='.$name.'&icon='.$imageUrl.'&status='.$status.'&remark='.$remark.'&amount='.$amount;
			$result = $this->curl_http(C('video_url').'l_prop/add?token='.$_SESSION["TOKEN"], $param);
			if($result['Success']){
				$this->success("添加成功！", U("Admin/LiveProp/index"));
			}else{
				return $this->error($result['Message']);
			}
		}
	}
	
	function edit(){
		$propId = I("get.propId", 0);
		
		$result = $this->curl_http(C('video_url').'l_prop/get?token='.$_SESSION["TOKEN"], 'propId='.$propId);
		$this->assign($result['ReturnData']);
		$this->display();
	}
	
	function edit_post(){
		$name = I("post.name", "");
		if(empty($name)){
			return $this->error("道具名称不能为空！");
		}
		
		$amount = I("post.amount", '');
		if(empty($amount)){
			return $this->error("汇闻币数量不能为空！");
		}
		if (!preg_match('/^[1-9]+[0-9]*$/', $amount)) {
			return $this->error("汇闻币数量不合法（整数）！");
		}
		
		if(!empty($_FILES)){
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     59614161 ;// 设置附件上传大小
			$upload->exts      =     array('jpeg', 'jpg', 'bmp', 'png');// 设置附件上传类型
			$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
			
			// 上传文件
			$info = $upload->upload();
			if(!$info) {// 上传错误提示错误信息
				return $this->error($upload->getError());
			}else{
				$savename = C('UPLOADPATH').$info['imageFile']['savepath'].$info['imageFile']['savename'];
				$fileName = 'lv'.time().$info['imageFile']['savename'];  // 自定义文件名
				$uploadRet = ImageV2::upload($savename, Conf::BUCKET, $fileName);
				\Think\Log::record('万象优图返回：'.json_encode($uploadRet), 'INFO');
				$icon = $uploadRet['data']['downloadUrl'];
			}
		}
		
		$propId = I("post.propId", 0);
		$status = I("post.status", 1);
		$remark = I("post.remark", "");
		
		$param = 'name='.$name.'&icon='.$icon.'&status='.$status.'&remark='.$remark.'&amount='.$amount.'&propId='.$propId;
		$result = $this->curl_http(C('video_url').'l_prop/update?token='.$_SESSION["TOKEN"], $param);
		if($result['Success']){
			$this->success("修改成功！", U("Admin/LiveProp/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
	
	function disable(){
		$propId = I("get.propId", 0);
		$status = 0;
		$postData = 'propId='.$propId.'&status='.$status;
		$result = $this->curl_http(C('video_url').'l_prop/update?token='.$_SESSION["TOKEN"], $postData);
		if($result['Success']){
			$this->success("停用成功！", U("Admin/LiveProp/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
	
	function enable(){
		$propId = I("get.propId", 0);
		$status = 1;
		$postData = 'propId='.$propId.'&status='.$status;
		$result = $this->curl_http(C('video_url').'l_prop/update?token='.$_SESSION["TOKEN"], $postData);
		if($result['Success']){
			$this->success("启用成功！", U("Admin/LiveProp/index"));
		}else{
			return $this->error($result['Message']);
		}
	}
}